package com.briup.jz.service.impl;

import com.briup.jz.bean.Order;
import com.briup.jz.bean.OrderExample;
import com.briup.jz.dao.OrderMapper;
import com.briup.jz.service.IOrderService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderImpl implements IOrderService {
    @Resource
    private OrderMapper orderMapper;
//    保存或咋修改订单
    @Override
    public void saveOrUpdate(Order order) throws CustomerException {
//        判断用户是否输入订单id
        if(order.getId()!=null){
            orderMapper.insert(order);
        }else {
            orderMapper.updateByPrimaryKey(order);
        }
    }
//根据主键id删除订单
    @Override
    public void deleteById(Long id) throws CustomerException {
        Order order=orderMapper.selectByPrimaryKey(id);
        if (order==null){
            throw  new CustomerException("没有这个订单");
        }
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Order> query(Long id, Long customerid, Long employeeid) {
        OrderExample example=new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        if (customerid!=null){
            criteria.andEmployeeIdEqualTo(customerid);
        }
        if (employeeid!=null){
            criteria.andIdEqualTo(employeeid);
        }
        return orderMapper.selectByExample(example);
    }

}
